package com.gigmissions.client;

import java.util.List;

import com.gigmissions.client.callbacks.LoginSignupCallbackHandler;
import com.gigmissions.client.dto.Artist;
import com.gigmissions.client.services.ArtistServiceAsync;
import com.gigmissions.client.services.LoginServiceAsync;
import com.gigmissions.client.ui.MainTabPanel;
import com.gigmissions.client.ui.presenters.MapPresenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class Main implements Command{

	
	private ArtistServiceAsync artistService;
	private LoginServiceAsync loginService;
	private MainTabPanel mainTabPanel;
	private LoginSignupCallbackHandler callback;
	private MapPresenter mapPresenter;
	
	@Inject
	public Main(MapPresenter mapPresenter,ArtistServiceAsync artistService,LoginServiceAsync loginService,LoginSignupCallbackHandler callback,MainTabPanel mainTabPanel){
		this.artistService = artistService;
		this.loginService = loginService;
		this.mainTabPanel = mainTabPanel;
		this.callback = callback;
		this.mapPresenter = mapPresenter;
	}
	
	@Override
	public void execute() {
		
		loginService.login(GWT.getHostPageBaseURL(), callback);
		
		artistService.getArtists(0, 9, new AsyncCallback<List<Artist>>(){
			@Override
			public void onFailure(Throwable caught) {
				//error
				Window.alert("Error getting artists " + caught.getMessage());
			}

			@Override
			public void onSuccess(List<Artist> result) {
				
				getMainTabPanel().getHomePanel().showActivityPanel(result);
				mapPresenter.present(result);
				
			}

			
			});
		
		
		getMainTabPanel().getMapWidget().checkResize();
			
		
		
	}
	
	private MainTabPanel getMainTabPanel() {
		// TODO Auto-generated method stub
		return mainTabPanel;
	}
	

}
